﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.Product.UC
{
    public partial class CategorySearchPropertyItems : System.Web.UI.UserControl
    {
        public int CategoryId { get; set; }
        public ShopCategory _category { get; set; }
        public string TagId { get; set; }
        public IList<ShopCategorySearchProperty> PropertyList { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId = string.IsNullOrEmpty(Request["categoryid"]) ? 0 : int.Parse(Request["categoryid"]);
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                _category = session.CreateQuery("from ShopCategory where Id=:cid").SetParameter("cid", CategoryId).UniqueResult<ShopCategory>();
                if (_category != null)
                {
                    rpt_son_category.DataSource = session.CreateQuery("from ShopCategory where ParentId=:pid").SetParameter("pid", _category.Id).List<ShopCategory>();
                    rpt_son_category.DataBind();
                }

            }
            rpt_property.DataSource = PropertyList;
            rpt_property.DataBind();


        }

        #region 绑定待付款订单的产品信息 + protected void rptOrder1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        /// <summary>
        /// 绑定待付款订单的产品信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //判断里层repeater处于外层repeater的哪个位置（ AlternatingItemTemplate，FooterTemplate，

            //HeaderTemplate，，ItemTemplate，SeparatorTemplate）
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("rpt_property_item") as Repeater;//找到里层的repeater对象
                ShopCategorySearchProperty property = (ShopCategorySearchProperty)e.Item.DataItem;//找到分类Repeater关联的数据项 

                if (property.ParentId > 0 && string.IsNullOrEmpty(Request[property.ParentId.ToString()]))
                {
                    e.Item.Visible = false;
                    return;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    IList<ShopCategorySearchPropertyItem> propertyItemList;


                    if (property.ParentId == 0)
                    {
                        propertyItemList = session.CreateQuery("from ShopCategorySearchPropertyItem where SearchPropertyId=:pid ").SetParameter("pid", property.Id).List<ShopCategorySearchPropertyItem>();
                    }
                    else
                    {
                        propertyItemList = session.CreateQuery("from ShopCategorySearchPropertyItem where SearchPropertyId=:pid and ParentId = :ppid  order by Sort  ").SetParameter("pid", property.Id).SetParameter("ppid", int.Parse(Request[property.ParentId.ToString()])).List<ShopCategorySearchPropertyItem>();
                    }

                    //if (propertyItemList.Count > 0)
                    //{
                    //    if (propertyItemList[0].ParentId > 0)
                    //    {
                    //        ShopCategorySearchPropertyItem item = session.Get<ShopCategorySearchPropertyItem>(propertyItemList[0].ParentId);
                    //        ShopCategorySearchProperty group = session.Get<ShopCategorySearchProperty>(item.SearchPropertyId);

                    //        e.Item.Visible = true;

                    //        //propertyItemList = propertyItemList.Where(p => p.ParentId == Int32.Parse(Request[group.Id.ToString()]));
                    //        for (int i = 0; i < propertyItemList.Count; i++)
                    //        {
                    //            if (propertyItemList[i].ParentId != Int32.Parse(Request[group.Id.ToString()]))
                    //            {
                    //                propertyItemList.Remove(propertyItemList[i]);
                    //            }
                    //        }

                    //    }

                    //}
                    rep.DataSource = propertyItemList;
                    rep.DataBind();
                }
            }
        }
        #endregion



        public string GetUrlHtml(int propertyId, int itemId)
        {
            string url = "href='/product/productlist.aspx?categoryid=" + CategoryId + "&orderby=" + Request["orderby"] + "&asc=" + Request["asc"] + "&";
            string cssHtml = "";

            foreach (ShopCategorySearchProperty p in PropertyList)
            {
                if (p.Id == propertyId)
                {
                    url += propertyId.ToString() + "=" + itemId + "&";

                }
                else
                {
                    url += p.Id.ToString() + "=" + Request[p.Id.ToString()] + "&";
                }
            }
            if (itemId.ToString() == (string.IsNullOrEmpty(Request[propertyId.ToString()]) ? "" : Request[propertyId.ToString()]))
            {
                cssHtml = "   class='acitve'";
            }

            return url + "'" + cssHtml;

        }

        public string GetSelectAllUrl(int propertyId)
        {
            string url = "href='/product/productlist.aspx?keyword=" + Request["keyword"] + "&categoryid=" + CategoryId + "&orderby=" + Request["orderby"] + "&asc=" + Request["asc"] + "&";
            string cssHtml = "";

            if (string.IsNullOrEmpty(Request[propertyId.ToString()]))
            {
                cssHtml = "   class='acitve'";
            }

            foreach (ShopCategorySearchProperty p in PropertyList)
            {
                if (p.Id == propertyId)
                {
                    url += p.Id.ToString() + "=&";
                }
                else
                {
                    url += p.Id.ToString() + "=" + Request[p.Id.ToString()] + "&";
                }
            }



            return url + "'" + cssHtml;
        }

        public string GetNavHtml()
        {
            string htmlStr = "";
            bool isSon = true;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                while (isSon)
                {
                    ShopCategory category = session.Get<ShopCategory>(CategoryId);
                    if (category != null)
                    {
                        htmlStr = "<li><span><a href='/product/productlist.aspx?categoryid=" + category.Id + "&tagid=" + TagId + "'>" + category.CategoryName + "</a></span></li> " + (string.IsNullOrEmpty(htmlStr) ? "" : "<li>></li>") + htmlStr;
                        if (category.ParentId == 0)
                        {
                            isSon = false;
                        }
                        else
                        {
                            CategoryId = category.ParentId;
                        }
                    }
                    else
                    {
                        isSon = false;
                    }
                }
            }
            return htmlStr;
        }
    }
}